{extend name="$base" /}
{block name="resources"}
<style>
.brand_img{
	width: 168px;
	height: 30px;
	text-align: center;
	vertical-align: middle;
}
.brand_img img{
	max-width: 100%;
	max-height: 100%;
}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th width="30%" style="text-align: left;">
			<button class="btn-common-delete" onclick="batchDelete()" style="margin:0 5px 0 0 !important;">批量删除</button>
			<button class="btn-common" onclick="addBrand()" style="margin:0 5px 0 0 !important;">添加品牌</button>
		</th>
		<th width="20%">
			<input type="text" id = 'search_text' placeholder="输入品牌名称" class="input-common"/>
			<input type="button" onclick="LoadingInfo(1)" value="搜索" class="btn-common"/>
		</th>
	</tr>
</table>
<table class="table-class">
	<colgroup>
		<col style="width: 2%;">
		<col style="width: 21%;">
		<col style="width: 26%;">
		<col style="width: 21%;">
		<col style="width: 10%;">
		<col style="width: 10%;">
		<col style="width: 10%;">
	</colgroup>
	<thead>
		<tr>
			<th>
				<i class="checkbox-common">
					<input type="checkbox"  />
				</i>
			</th>
			<th align="left">品牌名称</th>
			<th>图片</th>
			<th>品牌首字母</th>
 			<th>是否推荐</th>
 			<th>排序</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>
{/block}
{block name="script"}
<script type="text/javascript">
/* 跳转到添加品牌 */
function addBrand() {
	location.href = __URL("ADMIN_MAIN/goods/addgoodsbrand");
}

//批量删除
function batchDelete(){
	var brand_ids = [];
	$(".table-class tbody input[type='checkbox']:checked").each(function() {
		if (!isNaN($(this).val())) {
			brand_ids.push($(this).val());
		}
	});
	if(brand_ids.length==0){
		showTip("请选择需要操作的记录","warning");
		return false;
	}
	delBrand(brand_ids);
}

//加载数据
function LoadingInfo(page_index) {
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/goodsbrandlist')}",
		data : {
			"page_index" : page_index,
			"page_size" : $("#showNumber").val(),
			"search_text" : $("#search_text").val()
		},
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
					html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data["data"][i]["brand_id"] +'" ></i></td>';
					html += '<td align="left">' + data["data"][i]["brand_name"] + '</td>';				
					if(data["data"][i]["brand_pic"]==''){
						html += '<td></td>';
					}else{
						html += '<td><div class="brand_img"><img src="'+ __IMG(data["data"][i]["brand_pic"]) +'"></div></td>';
					}		
					html += '<td>' + data["data"][i]["brand_initial"] + '</td>';
					if(data['data'][i]['brand_recommend'] == 1){
						html += '<td ><a href="javascript:;" class="is_visible"  is_visible="1" onclick="updatetype('+data["data"][i]["brand_id"]+', this)"><img src="ADMIN_IMG/checked.png" alt="" width="15"/></a></td>';
					}else{
						html += '<td ><a href="javascript:;" class="is_visible"  is_visible="0" onclick="updatetype('+data["data"][i]["brand_id"]+', this)"><img src="ADMIN_IMG/un_checked.png" alt="" width="15"/></a></td>';
					}
					html += '<td style="text-align: center;">';
						html += '<input type="number" onchange="updatesort('+ data['data'][i]['brand_id'] +',this)"  class="sort input-common input-common-sort" name="categorySort" value="'+ data['data'][i]['sort'] +'" >';
					html += '</td>';
				//	html += '<td>'+data["data"][i]['category_id_1_name']+'&nbsp;'+data["data"][i]['category_id_2_name']+'&nbsp;'+data["data"][i]['category_id_3_name']+'</td>';
					html += '<td><a href="'+__URL('ADMIN_MAIN/goods/updategoodsbrand?brand_id=' + data["data"][i]["brand_id"] )+ '">修改</a>  <a href="javascript:delBrand(' + data["data"][i]["brand_id"] + ')">删除</a></td> ';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="9">暂无符合条件的数据记录</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

/* 删除品牌 */
function delBrand(brand_id) {
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/goods/deletegoodsbrand')}",
					data : { 'brand_id' : brand_id.toString() },
					dataType : "json",
					success : function(data) {
						if (data['code'] > 0) {
							LoadingInfo(getCurrentIndex(brand_id,'.table-class tbody'));
							showTip(data['message'],"success");
						}else{
							showTip(data['message'],"error");
						}
					}
				});
			},"取消,#f5f5f5,#666" : function(){
				$(this).dialog('close');
			}
		},
		contentText:"你确定删除吗？",
		title:"消息提醒"
	});
}

function updatetype(id,event){
	var type = $(event).attr("is_visible");	
	if(type == 1){
		var type_id = 0;
	}else{
		var type_id = 1;
	}
	$.ajax({
		url : "{:__URL('ADMIN_MAIN/goods/updateGoodsBrandType')}",
		type : "post",
		data : {
			"brand_id" : id,
			"brand_recommend" : type_id
		},
		success : function(data){
			if(data > 0){
				LoadingInfo($(".currentPage").html());
				showTip("操作成功","success");
			}
		}
	})
}

function updatesort(id,event){
	var sort = $(event).val();
	$.ajax({
		url : "{:__URL('ADMIN_MAIN/goods/updateGoodsBrandSort')}",
		type : "post",
		data : {
			"brand_id" : id,
			"sort" : sort
		},
		success : function(data){
			if(data > 0){
				LoadingInfo(1);
				showTip("操作成功","success");
			}
		}
	})
}
</script>
{/block}